package com.aaa.docter.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.aaa.docter.dao.SchedulingDao;
import com.aaa.docter.feign.UserFeign;
import com.aaa.docter.service.SchedulingService;
import com.aaa.docter.vo.SchedulingDto;
import com.aaa.docter.vo.SchedulingVo;
import com.aaa.entity.Scheduling;
import com.aaa.entity.User;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * @program: aaa-his-parent
 * @description:
 * @author: 闫克起2
 * @create: 2023-12-09 11:16
 **/
@Service
public class SchedulingServiceImpl implements SchedulingService {
    @Autowired
    private UserFeign userFeign;
    @Autowired
    private SchedulingDao schedulingDao;
    @Override
    public Result<Map<String,Object>> findScheduling(SchedulingVo schedulingVo) {
        //1. 根据条件查询值班医生的信息。
        Map<String,Object> params=new HashMap<>();
        params.put("userId",schedulingVo.getUserId());
        params.put("deptId",schedulingVo.getDeptId());
        List<User> users = userFeign.getUser(params);
        return findResult(schedulingVo,users);
    }

    private Result<Map<String, Object>> findResult(SchedulingVo schedulingVo, List<User> users) {
        //封装值班的时间
        //1.获取当前时间
        DateTime date = DateUtil.date();
        //2. 判断前端查询时是否传递时间
        if(StringUtils.hasText(schedulingVo.getQueryDate())){
            DateTime dateTime = DateUtil.parse(schedulingVo.getQueryDate(), "yyyy-MM-dd");
            int i = DateUtil.dayOfWeek(dateTime);//判断你传递的时间2是周一 1还是周日
            if(i==1){ //表示周日
                date=DateUtil.offsetDay(dateTime,1);
            }else{
                date=DateUtil.offsetDay(dateTime,-1);
            }
        }

        //3.获取指定日期所在周的周一日期和周日日期
        DateTime begin = DateUtil.beginOfWeek(date);
        DateTime end = DateUtil.endOfWeek(date);
        //4.封装到SchedulingVO中
        schedulingVo.setBeginDate(DateUtil.format(begin,"yyyy-MM-dd"));
        schedulingVo.setEndDate(DateUtil.format(end,"yyyy-MM-dd"));
        //5. 查询排班表
        List<Scheduling> schedulings= selectSchedulingByCon(schedulingVo);
        List<SchedulingDto> schedulingDtos=new ArrayList<>();
        //6.遍历值班人员信息
        for(User user:users){
             SchedulingDto dto1=new SchedulingDto(user.getUserId(),user.getDeptId(),"1",initMap(begin));
             SchedulingDto dto2=new SchedulingDto(user.getUserId(),user.getDeptId(),"2",initMap(begin));
             SchedulingDto dto3=new SchedulingDto(user.getUserId(),user.getDeptId(),"3",initMap(begin));
             for(Scheduling scheduling:schedulings){
                 //值班人的id
                 Long userId = scheduling.getUserId();
                 //获取值班的时间
                 String schedulingDay = scheduling.getSchedulingDay();
                 //值班的时间段
                 String subsectionType = scheduling.getSubsectionType();
                 //值班的类型
                 String schedulingType = scheduling.getSchedulingType();
                 if(user.getUserId().equals(userId)){
                        switch (subsectionType){
                            case "1":
                                Map<String, String> record1 = dto1.getRecord();
                                record1.put(schedulingDay,schedulingType);
                                break;
                            case "2":
                                Map<String, String> record2 = dto2.getRecord();
                                record2.put(schedulingDay,schedulingType);
                                break;
                            case "3":
                                Map<String, String> record3 = dto3.getRecord();
                                record3.put(schedulingDay,schedulingType);
                                break;
                        }
                 }
             }
             dto1.setSchedulingType(dto1.getRecord().values());
             dto2.setSchedulingType(dto2.getRecord().values());
             dto3.setSchedulingType(dto3.getRecord().values());
             schedulingDtos.add(dto1);
             schedulingDtos.add(dto2);
             schedulingDtos.add(dto3);
        }

        //7. 获取这周日期头
        List<String> labelNames=initLabelNames(begin);
        Map<String,Object> map=new HashMap<>();
        map.put("labelNames",labelNames);
        map.put("beginDate",DateUtil.format(begin,"yyyy-MM-dd"));
        map.put("endDate",DateUtil.format(end,"yyyy-MM-dd"));
        map.put("tableData",schedulingDtos);
        return new Result<>(200,"查询成功",map);
    }

    private List<String> initLabelNames(DateTime begin) {
        List<String> labelNames=new ArrayList<>();
        for(int i=0;i<7;i++) {
            DateTime dateTime = DateUtil.offsetDay(begin, i);
            String label=DateUtil.format(dateTime,"yyyy-MM-dd")+week(i);
            labelNames.add(label);
        }
        return labelNames;
    }

    private String week(int i) {
        String week="";
        switch (i){
            case 0:
                week="(周一)";
                break;
            case 1:
                week="(周二)";
                break;
            case 2:
                week="(周三)";
                break;
            case 3:
                week="(周四)";
                break;
            case 4:
                week="(周五)";
                break;
            case 5:
                week="(周六)";
                break;
            case 6:
                week="(周日)";
                break;
        }
        return week;
    }

    private  Map<String, String> initMap(DateTime begin) {
        //必须使用treeMap
        Map<String,String> map=new TreeMap<>();
        for(int i=0;i<7;i++){
            DateTime dateTime = DateUtil.offsetDay(begin, i);
            map.put(DateUtil.format(dateTime,"yyyy-MM-dd"),"");
        }
        return map;
    }

    private List<Scheduling> selectSchedulingByCon(SchedulingVo schedulingVo) {
        QueryWrapper<Scheduling> wrapper=new QueryWrapper<>();
        if(Objects.nonNull(schedulingVo.getUserId())){
            wrapper.eq("user_id",schedulingVo.getUserId());
        }
        if(Objects.nonNull(schedulingVo.getDeptId())){
            wrapper.eq("dept_id",schedulingVo.getDeptId());
        }
        if(StringUtils.hasText(schedulingVo.getBeginDate())){
            wrapper.between("scheduling_day",schedulingVo.getBeginDate(),schedulingVo.getEndDate());
        }
        return schedulingDao.selectList(wrapper);
    }


    public static void main(String[] args) {
        DateTime parse = DateUtil.parse("2023-12-11", "yyyy-MM-dd");

    }
}














